import os
import pygame
import math
import time
from pygame.locals import *
import assaultMarine
import tree
import vector
#------------------------------------------------------------------------------
# Name: loadImageFile()
# Desc: klasa zerznieta z tutoriala w celu ladowania obrazkow do sprite'ow
#------------------------------------------------------------------------------

def loadImageFile( fileName, useColorKey = False ):
    try:
        image = pygame.image.load( "F:\semestr VIII\Programowanie silnika\projekt\pygengine\src\stery1\\" + fileName )
    except pygame.error, message:
        print "Cannot load image:", "" + "F:\semestr VIII\Programowanie silnika\projekt\pygengine\src\stery1\\" + fileName
        raise SystemExit, message
    
    
    # To speed things up, convert the images to SDL's internal format.
    image = image.convert()

    if useColorKey is True:
        # Use the color of the pixel located at (0,0) as our colorkey
        colorkey = image.get_at( (0,0) )
        image.set_colorkey( colorkey, RLEACCEL )

    # Return the image
    return image
    


#------------------------------------------------------------------------------
# Name: main()
# Desc: 
#------------------------------------------------------------------------------

def main():

    # Initialize pygame and create a window or screen to render to
    pygame.init()
    screen = pygame.display.set_mode( (640, 480) )

    # Create a backgound for our screen
    background = pygame.Surface( screen.get_size() )
    background = background.convert()
    background.fill( (90, 110, 80) )

    #create clickable menus
    #TODO
    podpis= pygame.font.SysFont("Arial",12)
    napisy=screen.subsurface(pygame.Rect(10,10,300,300))
    napisy.fill((0xff,0xff,0xff))
    podpisy = podpis.render("TEST",0,(0xFF,0x00,0xFF))
    pygame.display.update()
    spriteTable=list()
    marineSquad = list()
    terrainList = list()
    # Initialize our sprite object
    sprite = assaultMarine.AssaultMarine("Cassius",0,marineSquad)
    sprite.setAsLeader()
    spriteTable.append(sprite)
    marineSquad.append(sprite)
    
    sprite2 = assaultMarine.AssaultMarine("Decurius",sprite,marineSquad)
    sprite2.setAsFollower()
    
    spriteTable.append(sprite2)
    marineSquad.append(sprite2)

    sprite3 = assaultMarine.AssaultMarine("Tertius",sprite2,marineSquad)
    sprite3.setAsFollower()
    spriteTable.append(sprite3)
    marineSquad.append(sprite3)
    sprite4 = assaultMarine.AssaultMarine("Qatrus",sprite3,marineSquad)
    sprite4.setAsFollower()
    spriteTable.append(sprite2)
    marineSquad.append(sprite2)
    
    sprite5 = assaultMarine.AssaultMarine("Quintus",sprite4,marineSquad)
    sprite5.setAsFollower()
    spriteTable.append(sprite2)
    marineSquad.append(sprite2)
    
    
    sprite1 = tree.Tree("Dzefko")
    spriteTable.append(sprite1)
    terrainList.append(sprite1)
    
    allsprites = pygame.sprite.RenderPlain(sprite5,sprite4,sprite3,sprite2,sprite)
    
    
    
    # Initialize a clock for animating the sprite's motion
    clock = pygame.time.Clock()
    selectedSprite=0
    selectedSprite = sprite
    # Ok... we're done initializing! Time to enter the script's
    # main loop and start drawing our sprite.

    while 1:
        clock.tick( 60 )

        # Handle input events
        pygame.event.pump()
        keyinput = pygame.key.get_pressed()
        if keyinput[K_ESCAPE] or pygame.event.peek(QUIT):
            break
        mouseinput = pygame.mouse.get_pressed()
        mousepos=pygame.mouse.get_pos()
        spriFlag = False
        
        if mouseinput == (1,0,0):
            spriFlag=True
            
            mousepos=pygame.mouse.get_pos()
            for spri in spriteTable:
                if (mousepos[0] > spri.rect.left) and (mousepos[0] < spri.rect.right) and mousepos[1]>spri.rect.top and mousepos[1]<spri.rect.bottom and spri.selectable:
                    selectedSprite=spri
                    print "Wybrano " + spri.name
                    spriFlag=False
                    time.sleep(0.3)
            
        elif mouseinput==(0,0,1) and selectedSprite<>0:
                selectedSprite.targetPos.x = mousepos[0]
                selectedSprite.targetPos.y = mousepos[1]
                time.sleep(0.1)
                print "Zmiana Celu!"
                time.sleep(0.1)
        # Update our sprite object
        if spriFlag:
         selectedSprite = 0
         print "Odznaczono jednostke"
         spriflag=False

        #for spri in spriteTable:
        #    for spri2 in spriteTable:
        #        if spri.rect.colliderect(spri2.rect)==1 and spri!=spri2:
        #            print "kolizja!"
        
        allsprites.update(allsprites.sprites())
        
        # Display the background
        screen.blit( background, (0, 0) )
        allsprites.draw( screen )
        if selectedSprite <>0:
            targetDis = pygame.draw.ellipse(screen,(0xFF,0x00,0x00),pygame.Rect(selectedSprite.targetPos.x,selectedSprite.targetPos.y,10.0,10.0),0)
            speedVect=pygame.draw.aaline(screen,(0xff,0x00,0x00),selectedSprite.rect.center,(selectedSprite.rect.center[0]+10.0*selectedSprite.x_velocity,selectedSprite.rect.center[1]+10.0*selectedSprite.y_velocity))
            forceVect=pygame.draw.aaline(screen,(0x00,0x00,0xff),selectedSprite.rect.center,(selectedSprite.rect.center[0]+1000.0*selectedSprite.steeringForce.x,selectedSprite.rect.center[1]+1000.0*selectedSprite.steeringForce.y))
	    #TODO wyswietlanie steering force ssie - steering force zwraca wektor zerowy
	    #print 1000.0*selectedSprite.steeringForce.x,1000.0*selectedSprite.steeringForce.y
	    #print 10.0*selectedSprite.x_velocity,10.0*selectedSprite.y_velocity

            
        else:
            
            targetDis=0
        
        # Display the sprite
        pygame.display.flip()

# This calls the 'main' function when this script is executed
if __name__ == '__main__': main()
